package com.ossjk.qlh.system.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ossjk.qlh.system.entity.User;
import com.ossjk.qlh.system.entity.UserRole;
import com.ossjk.qlh.system.mapper.UserMapper;
import com.ossjk.qlh.system.service.IUserRoleService;
import com.ossjk.qlh.system.service.IUserService;
import com.ossjk.qlh.system.vo.UserVo;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-01-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
	@Autowired
	private IUserRoleService iUserRoleService;

	@Override
	public boolean saveVo(UserVo userVo) {
		User user = userVo;
		// 新增用户
		if (this.save(user)) {
			// 新增用户角色
			UserRole userRole = new UserRole();
			userRole.setRid(userVo.getRid());
			userRole.setUid(userVo.getId());
			return iUserRoleService.save(userRole);
		} else {
			// TODO Auto-generated method stub
			return false;
		}
	}

	@Override
	public boolean updateVo(UserVo userVo) {
		User user = userVo;
		// 更新用户
		if (this.updateById(user)) {
			// 更新用户角色
			UpdateWrapper<UserRole> updateWrapper = new UpdateWrapper();
			updateWrapper.set("rid", userVo.getRid());
			updateWrapper.eq("uid", userVo.getId());
			return iUserRoleService.update(updateWrapper);
		}
		return false;
	}

	@Override
	public boolean removeUserRoleByIds(List<Serializable> ids) {
		// 删除用户
		this.removeByIds(ids);
		// 删除用户和角色关联
		QueryWrapper<UserRole> queryWrapper2 = new QueryWrapper();
		queryWrapper2.in("uid", ids);
		return iUserRoleService.remove(queryWrapper2);
	}

	@Override
	public UserVo getInfoById(String id) {
		// TODO Auto-generated method stub
		return baseMapper.selectInfoById(id);
	}

	@Override
	public Page pageVo(Page page, QueryWrapper<User> queryWrapper) {
		// TODO Auto-generated method stub
		return baseMapper.pageVo(page, queryWrapper);
	}

}
